package cn.zyjblogs.server.user.controller;

import cn.zyjblogs.server.user.dto.AuthCodeDto;
import cn.zyjblogs.server.user.dto.AuthorizationCodeDto;
import cn.zyjblogs.server.user.dto.OAuth2AccessTokenDto;
import cn.zyjblogs.server.user.service.AuthService;
import cn.zyjblogs.server.user.vo.OAuth2AccessTokenVo;
import cn.zyjblogs.starter.common.entity.response.ResponseObject;
import cn.zyjblogs.starter.common.entity.response.ResponseResult;
import cn.zyjblogs.starter.web.apiversion.ApiVersion;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhuyijun
 */
@Api(tags = {"token管理"})
@RestController
@RequiredArgsConstructor
@ApiVersion(1)
@RequestMapping("/{v}/auth")
public class AuthController {
    private final AuthService authService;

    @ApiOperation(value = "刷新token", notes = "刷新token")
    @PostMapping("/refresh/token")
    @ApiVersion(1)
    public ResponseObject<OAuth2AccessTokenVo> refreshToken(@RequestBody @Validated OAuth2AccessTokenDto oAuth2AccessTokenDto) {
        return ResponseResult.success(authService.refreshToken(oAuth2AccessTokenDto));
    }

    @ApiOperation(value = "检测token", notes = "检测token")
    @PostMapping("/check/token")
    @ApiVersion(1)
    public ResponseObject<OAuth2AccessTokenVo> checkToken(@RequestBody @Validated OAuth2AccessTokenDto oAuth2AccessTokenDto) {
        return ResponseResult.success(authService.checkToken(oAuth2AccessTokenDto));
    }

    @ApiOperation(value = "获取授权码", notes = "获取授权码")
    @PostMapping("/authorize")
    @ApiVersion(1)
    public ResponseObject getAuthorizationCode(@RequestBody @Validated AuthorizationCodeDto authorizationCodeDto) {
        return ResponseResult.success(authService.getAuthorizationCode(authorizationCodeDto));
    }

    @ApiOperation(value = "通过授权码获取token", notes = "获取授权码")
    @PostMapping("/authorize/code")
    @ApiVersion(1)
    public ResponseObject<OAuth2AccessTokenVo> getTokenByAuthorizationCode(@RequestBody @Validated AuthCodeDto authCodeDto) {
        return ResponseResult.success(authService.getTokenByAuthorizationCode(authCodeDto));
    }

}
